<?php
/**
 * 操作日志
 * @author chengx impl@wanthings.com
 */
class OptionLog extends BaseActiveRecord
{

    CONST OPTION_TYPE_ADD = 1;//添加
    CONST OPTION_TYPE_UPDATE = 2;//修改
    CONST OPTION_TYPE_DELETED = 3;//删除

    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }
    public function tableName()
    {
        return '{{option_log}}';
    }
    public function getProperty()
    {
        $result =  array();
        $criteria = new CDbCriteria;
        $criteria->order = 'id desc';
        $data = $this->findAll($criteria);
        if (!empty($data)) {
            foreach ($data as $key => $value) {
                $extended = CJSON::decode($value['extended']);
                if (isset($extended['old']) && !empty($extended['old']['property'])) {
                    $temp['sid'] = $extended['old']['sid'];
                    $temp['property'] = CJSON::decode($extended['old']['property']);
                    $result[] = $temp;
                }
            }
        }
        return $result;
    }

    public function add($arr)
    {
        $this->setIsNewRecord(true);
        $this->id = null;
        $this->user_id = $arr['user_id'];
        $this->log_msg = $arr['log_msg'];
        $this->module_id = $arr['module_id'];
        $this->type = $arr['type'];
        $this->extended = CJSON::encode($arr['extended']);
        $this->ctime = time();
        return $this->save();
    }
    /**
    *获取操作日志的年月列表
    */
    public function getOperate()
    {
        $result['data'] = array();
        $time = $this->JudgeTime();
        if (!empty($time)) {
            $count = $time['count'];
            $big_year = intval($time['big_year']);
            $big_month = intval($time['big_month']);
            $small_year = intval($time['small_year']);
            $small_month = intval($time['small_month']);
            $less = intval($big_year-$small_year);
            $year = $big_year;
            $month = $big_month;
            for($i=intval("0");$i<=intval($count);$i++) {
                $temp = array();
                $temp['name'] = $year.'年'.$month.'月';
                $temp['year'] = $year;
                $temp['month'] = $month;
                $result['data'][] = $temp;
                if ($month-1<=0) {
                    $year--;
                    $month=12;
                }else{
                    $month--;
                }
            }
        }
        return $result;
    }
    /**
    *判断含有操作日志的年月时间
    */
    public function JudgeTime()
    {
        $result = array();
        $less_year = intval("0");
        $less_month = intval("0");
        $big_year = intval("0");
        $big_month = intval("0");
        $small_year = intval("0");
        $small_month = intval("0");
        $start_time = $this->operateBig();
        $end_time = $this->operateSmall();
        if (!empty($start_time)&&!empty($end_time)) {
            $big_year =intval(date('Y', $start_time));
            $big_month =intval(date('m', $start_time));
            $small_year = intval(date('Y', $end_time));
            $small_month = intval(date('m', $end_time));
            //var_dump($big_year);var_dump($small_year);exit();
            $result['big_year'] = $big_year;
            $result['small_year'] = $small_year;
            $result['big_month'] = $big_month;
            $result['small_month'] = $small_month;
            $less_year = $big_year-$small_year;

            if ($less_year==intval("0")){
                $less_month = $big_month-$small_month+intval("1");
                $result['count'] = $less_month;
            } elseif ($less_year==intval("1")) {
                $less_month = intval("12")-$small_month+$big_month+intval("1");
                $result['count'] = $less_month;
            } elseif($less_year>=intval("2")) {
                $less_month = $intval("12")-$small_month+$big_month+intval("12")*($less_year-intval("1"))+intval("1");
                $result['count'] = $less_month;
            } else {
                $result = array();
                return $result;
            }
        }
        return $result;
    }
    /**
    *查找含有操作日志的最早时间
    */
    public function operateSmall()
    {
        $result = 0;
        $criteria = new CDbCriteria;
        $criteria->order = 'ctime asc';
        $data = $this->find($criteria);
        if (!empty($data)) {
            $small_time = $data->ctime;
            $result = $small_time;
        }
        return $result;
    }
    /**
    *查找含有操作日志的最新时间
    */
    public function operateBig()
    {
        $result = 0;
        $criteria = new CDbCriteria;
        $criteria->order = 'ctime desc';
        $data = $this->find($criteria);
        if (!empty($data)) {
            $big_time = $data->ctime;
            $result = $big_time;
        }
        return $result;
    }
    /**
    *查找指定年月的操作日志
    */
    public function getList($attr =array(), $page, $page_size)
    {
        $result['data'] = array();
        $result['pages'] = array();
        $criteria = new CDbCriteria;
        $criteria->order = 'id desc';
        if (!empty($attr)) {
            if (!empty($attr['day']) && !empty($attr['month']) && !empty($attr['year'])) {
                $max_month = $attr['month'];
                $max_year = $attr['year'];
                $max_day = $attr['day']+1;
                if ($attr['day'] == 31 || !in_array($attr['month'], array(1,3,5,7,8,10,12))&&$attr['day']==30 ||$attr['month']==2&&$attr['day']==28) {
                    $max_month = $attr['month']+1;
                    $max_day = 1;
                }
                if ($attr['month'] == 12) {
                    $max_month = 1;
                    $max_year = $attr['year']+1;
                }
                $start_time = strtotime($attr['year'].'-'.$attr['month'].'-'.$attr['day']);
                $end_time = strtotime($max_year.'-'.$max_month.'-'.$max_day);
                $criteria->addCondition('ctime>='.$start_time);
                $criteria->addCondition('ctime<'.$end_time);
            } else if (!empty($attr['month']) && !empty($attr['year'])) {
                $max_month = $attr['month'];
                $max_year = $attr['year'];
                $max_day = 0;
                if ($attr['month'] == 12) {
                    $max_month = 1;
                    $max_year = $attr['year']+1;
                }

                $start_time = strtotime($attr['year'].'-'.$attr['month']);
                $end_time = strtotime($max_year.'-'.$max_month);

                $criteria->addCondition('ctime>='.$start_time);
                $criteria->addCondition('ctime<'.$end_time);
            }

            if (!empty($attr['u_id'])) {
                $criteria->addColumnCondition(array('user_id'=>$attr['u_id']));
            }

            if (!empty($attr['module_id'])) {
                $criteria->addColumnCondition(array('module_id'=>$attr['module_id']));
            }
            if(!empty($attr['keyword'])){
                $criteria->addSearchCondition('log_msg',$attr['keyword']);
            }
        }
        $count = $this->count($criteria);
        if (!empty($page)||!empty($page_size)) {
            $pager = new CPagination;
            $pager->validateCurrentPage = false;
            $pager->setItemCount($count);
            $pager->setCurrentPage($page - 1);
            $pager->setPageSize($page_size);
            $pager->applyLimit($criteria);
            $result['pages']=$pager;
        }
        $data=$this->findAll($criteria);
        if (!empty($data)) {
            foreach ($data as $key => $value) {
                $temp = array();
                $temp = $value->attributes;
                $temp['user_name'] = $value['user_id'];
                //var_dump($temp);exit();
                $user = Manager::model()->getArrById($temp['user_id']);
                if (!empty($user['name'])) {
                    $temp['user_name'] = $user['name'];
                }
                $result['data'][] = $temp;
            }
        }
        return $result;
    }
    public function getDetail($id){
        $result = array();
        if (!empty($id)) {
            $criteria = new CDbCriteria;
            $criteria->addColumnCondition(array('id'=>$id));
            $data = $this->find($criteria);
            if (!empty($data)) {
                $result = $data->attributes;
            }
        }
        return $result;
    }
    public function searchOperate($content ,$page=0, $page_size=0)
    {
        $result['data'] = array();
        $result['pager'] = array();
        $criteria = new CDbCriteria;
        $criteria->order = 'ctime desc';
        if (!empty($content)) {
            $criteria->addSearchCondition('log_msg',$content);
        }
        $count = $this->count($criteria);
        if (!empty($page)||!empty($page_size)) {
            $pager = new CPagination;
            $pager->validateCurrentPage = false;
            $pager->setItemCount($count);
            $pager->setCurrentPage($page - 1);
            $pager->setPageSize($page_size);
            $pager->applyLimit($criteria);
            $result['pager']=$pager;
        }
        $data=$this->findAll($criteria);
        if (!empty($data)) {
            foreach ($data as $key => $value) {
                $temp = array();
                $temp = $value->attributes;
                $user = Manager::model()->getArrById($temp['user_id']);
                if (!empty($user['name'])) {
                    $temp['user_name'] = $user['name'];
                }
                $result['data'][] = $temp;
            }
        }
        return $result;
    }
}